/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.scene.web;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.modules.scene.entity.SceneSecurityGroup;
import com.lczyfz.istep.modules.scene.entity.SceneSecurityGroupRule;
import com.lczyfz.istep.modules.scene.service.SceneSecurityGroupRuleService;
import com.lczyfz.istep.modules.scene.service.SceneSecurityGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 安全组管理Controller
 * @author dh
 * @version 2017-03-28
 */
@Controller
@RequestMapping(value = "${adminPath}/scene/sceneSecurityGroup")
public class SceneSecurityGroupController extends BaseController {

	@Autowired
	private SceneSecurityGroupService sceneSecurityGroupService;
	@Autowired
	private SceneSecurityGroupRuleService sceneSecurityGroupRuleService;
	@ModelAttribute
	public SceneSecurityGroup get(@RequestParam(required=false) String id) {
		SceneSecurityGroup entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sceneSecurityGroupService.get(id);
		}
		if (entity == null){
			entity = new SceneSecurityGroup();
		}
		return entity;
	}
	
	@RequiresPermissions("scene:sceneSecurityGroup:view")
	@RequestMapping(value = {"list", ""})
	public String list(SceneSecurityGroup sceneSecurityGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SceneSecurityGroup> page = sceneSecurityGroupService.findPage(new Page<SceneSecurityGroup>(request, response), sceneSecurityGroup); 
		model.addAttribute("page", page);
		return "modules/scene/sceneSecurityGroupList";
	}

	@RequiresPermissions("scene:sceneSecurityGroup:view")
	@RequestMapping(value = "form")
	public String form(SceneSecurityGroup sceneSecurityGroup, Model model) {
		model.addAttribute("sceneSecurityGroup", sceneSecurityGroup);
		return "modules/scene/sceneSecurityGroupForm";
	}

	@RequiresPermissions("scene:sceneSecurityGroup:edit")
	@RequestMapping(value = "save")
	public String save(SceneSecurityGroup sceneSecurityGroup, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sceneSecurityGroup)){
			return form(sceneSecurityGroup, model);
		}
		logger.debug("dingh===！"+sceneSecurityGroup.getName());

		if(sceneSecurityGroupService.get(sceneSecurityGroup)==null)
		{
			logger.debug("插入！"+sceneSecurityGroup.getName());

			//插入
			if(sceneSecurityGroupService.findSecurityGroupByName(sceneSecurityGroup)!=null)
			{
				addMessage(redirectAttributes, "添加安全组'"+sceneSecurityGroup.getName()+"'失败，该安全组已存在");
			}else {
//				try{
//					SecuritygroupImpl securitygroupImpl = new SecuritygroupImpl();
//					Securitygroup temp=new Securitygroup();
//					temp.setSecuritygroupName(sceneSecurityGroup.getName());
//					temp.setDescription(sceneSecurityGroup.getRemarks());
//					Securitygroup securitygroup = securitygroupImpl.createSecuritygroup(temp);
//
////					ArrayList<SecuritygroupRule> securitygroupSr=securitygroup.getsecuritygroupSr();
////					for(SecuritygroupRule securitygroupRule:securitygroupSr)
////					{
////						SceneSecurityGroupRule sceneSecurityGroupRule=new SceneSecurityGroupRule();
////						sceneSecurityGroupRule.setId(securitygroupRule.getSrId());
////						sceneSecurityGroupRule.setDirection(securitygroupRule.getSrDirection());
////						sceneSecurityGroupRule.setTenantId(securitygroupRule.getSrTenantId());
////						sceneSecurityGroupRule.setProjectId(securitygroupRule.getSrProjectId());
////
////						sceneSecurityGroupRule.setEthertype(securitygroupRule.getSrEthertype());
////
////						sceneSecurityGroupRule.setProtocol(securitygroupRule.getSrProtocol());
////
////						sceneSecurityGroupRule.setSecurityGroupId(securitygroupRule.getSrSecurityGroupId());
////
////						sceneSecurityGroupRule.setRemoteGroupId(securitygroupRule.getSrRemoteGroupId());
////
////						sceneSecurityGroupRule.setRemoteIpPrefix(securitygroupRule.getSrRemoteIpPrefix());
////
////						sceneSecurityGroupRule.setToPort(securitygroupRule.getSrPortMax());
////
////						sceneSecurityGroupRule.setFromPort(securitygroupRule.getSrPortMin());
////
////						sceneSecurityGroupRuleService.preInsert(sceneSecurityGroupRule);
////						sceneSecurityGroupRuleService.insert(sceneSecurityGroupRule);
////					}
//
//					sceneSecurityGroup.setTenantId(securitygroup.getSecuritygroupTenantId());
//					sceneSecurityGroup.setProjectId(securitygroup.getSecuritygroupProjectId());
//					sceneSecurityGroup.setId(securitygroup.getSecuritygroupId());
//					sceneSecurityGroupService.preInsert(sceneSecurityGroup);
//					sceneSecurityGroupService.insert(sceneSecurityGroup);
//					addMessage(redirectAttributes, "添加安全组成功");
//				}catch (RuntimeException e){
//					addMessage(redirectAttributes, "添加安全组失败");
//
//				}
				sceneSecurityGroupService.save(sceneSecurityGroup);
				addMessage(redirectAttributes, "添加安全组成功");
			}
		}else{
			//修改
			logger.debug("修改！");
//			try{
//				SecuritygroupImpl securitygroupImpl = new SecuritygroupImpl();
//				Securitygroup temp=new Securitygroup();
//				temp.setSecuritygroupName(sceneSecurityGroup.getName());
//				temp.setDescription(sceneSecurityGroup.getRemarks());
//				securitygroupImpl.updateSecuritygroup(sceneSecurityGroup.getId(),temp);
//
//				sceneSecurityGroupService.save(sceneSecurityGroup);
//				addMessage(redirectAttributes, "添加安全组成功");
//			}catch (RuntimeException e){
//				addMessage(redirectAttributes, "添加安全组失败");
//
//			}
			sceneSecurityGroupService.save(sceneSecurityGroup);
			addMessage(redirectAttributes, "添加安全组成功");

		}

		return "redirect:"+Global.getAdminPath()+"/scene/sceneSecurityGroup/?repage";
	}
	
	@RequiresPermissions("scene:sceneSecurityGroup:edit")
	@RequestMapping(value = "delete")
	public String delete(SceneSecurityGroup sceneSecurityGroup, RedirectAttributes redirectAttributes) {
//		try{
//			SecuritygroupImpl securitygroupImpl = new SecuritygroupImpl();
//			securitygroupImpl.deleteSecuritygroup(sceneSecurityGroup.getId());
//			sceneSecurityGroupService.delete(sceneSecurityGroup);
//			addMessage(redirectAttributes, "删除安全组成功");
//		}catch (RuntimeException e){
//			addMessage(redirectAttributes, "删除安全组失败");
//
//		}
		sceneSecurityGroupService.delete(sceneSecurityGroup);
		addMessage(redirectAttributes, "删除安全组成功");
		return "redirect:"+Global.getAdminPath()+"/scene/sceneSecurityGroup/?repage";
	}

	/**
	 * 安全组-安全组规则列表
	 * @param sceneSecurityGroup
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("scene:sceneSecurityGroup:view")
	@RequestMapping(value = {"goSecurityGroupRule"})
	public String goSecurityGroupRule(SceneSecurityGroup sceneSecurityGroup, HttpServletRequest request, HttpServletResponse response, Model model) {
		SceneSecurityGroupRule sceneSecurityGroupRule=new SceneSecurityGroupRule();
		sceneSecurityGroupRule.setSecurityGroupId(sceneSecurityGroup.getId());
		Page<SceneSecurityGroupRule> page = sceneSecurityGroupRuleService.findPage(new Page<SceneSecurityGroupRule>(request, response), sceneSecurityGroupRule);
		model.addAttribute("page", page);
		model.addAttribute("sceneSecurityGroupId", sceneSecurityGroup.getId());
		return "modules/scene/sceneSecurityGroupRuleList";
	}
	/**
	 * 安全组规则删除
	 * @param securityGroupRuleId
	 * @param sceneSecurityGroupId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("scene:sceneSecurityGroup:view")
	@RequestMapping(value = {"deleteRule"})
	public String deleteRule(String securityGroupRuleId, String sceneSecurityGroupId, HttpServletRequest request, HttpServletResponse response, Model model) {
		logger.debug("page===deleteRule [{}]");

//		SecuritygroupRuleImpl srtest = new SecuritygroupRuleImpl();
//		srtest.deleteSecuritygroupRule(securityGroupRuleId);

		SceneSecurityGroupRule temp=new SceneSecurityGroupRule();
		temp.setId(securityGroupRuleId);
		sceneSecurityGroupRuleService.delete(temp);
		SceneSecurityGroupRule sceneSecurityGroupRule=new SceneSecurityGroupRule();
		sceneSecurityGroupRule.setSecurityGroupId(sceneSecurityGroupId);
		Page<SceneSecurityGroupRule> page = sceneSecurityGroupRuleService.findPage(new Page<SceneSecurityGroupRule>(request, response), sceneSecurityGroupRule);
		model.addAttribute("page", page);

		model.addAttribute("sceneSecurityGroupId", sceneSecurityGroupId);

		return "modules/scene/sceneSecurityGroupRuleList";
	}

	/**
	 * 添加规则
	 * @param sceneSecurityGroupId
	 * @param model
	 * @return
	 */

	@RequiresPermissions("scene:sceneSecurityGroup:view")
	@RequestMapping(value = "addRule")
	public String addRule(String sceneSecurityGroupId, Model model) {
		model.addAttribute("sceneSecurityGroupId", sceneSecurityGroupId);

		List<SceneSecurityGroup> securityGroupList=sceneSecurityGroupService.findList(new SceneSecurityGroup());
		model.addAttribute("securityGroupList", securityGroupList);

		return "modules/scene/sceneSecurityGroupRuleForm";
	}

	/**
	 * 安全组规则保存
	 * @param sceneSecurityGroupRule
	 * @param sceneSecurityGroupId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("scene:sceneSecurityGroup:view")
	@RequestMapping(value = {"saveRule"})
	public String saveRule(SceneSecurityGroupRule sceneSecurityGroupRule,String remoteChoose,String portChoose,  String port,String sceneSecurityGroupId, HttpServletRequest request, HttpServletResponse response, Model model) {
		logger.debug("page===Protocol [{}]", sceneSecurityGroupRule.getProtocol());
		logger.debug("page===port [{}]", port);

		logger.debug("page===sceneSecurityGroupId [{}]", sceneSecurityGroupRule.getSecurityGroupId());
//		String id = IdGen.uuid();
//		sceneSecurityGroupRule.setId(id);

//		SecuritygroupRuleImpl srtest = new SecuritygroupRuleImpl();
//
//		SecuritygroupRule srtt = new SecuritygroupRule();
//		srtt.setSrDirection(sceneSecurityGroupRule.getDirection());
//		srtt.setSrEthertype(sceneSecurityGroupRule.getEthertype());
//		srtt.setSrProtocol(sceneSecurityGroupRule.getProtocol());
//		srtt.setSrSecurityGroupId(sceneSecurityGroupRule.getSecurityGroupId());
//		if(remoteChoose.equals("1"))
//		{
//			srtt.setSrRemoteGroupId(sceneSecurityGroupRule.getRemoteGroupId());
//			sceneSecurityGroupRule.setRemoteIpPrefix(null);
//		}
//		else {
//			srtt.setSrRemoteIpPrefix(sceneSecurityGroupRule.getRemoteIpPrefix());
//			sceneSecurityGroupRule.setRemoteGroupId(null);
//		}
//
//		if(portChoose.equals("1"))
//		{
//			srtt.setSrPortMax(sceneSecurityGroupRule.getToPort());
//			srtt.setSrPortMin(sceneSecurityGroupRule.getFromPort());
//		}else {
//			srtt.setSrPortMax(port);
//			srtt.setSrPortMin(port);
//			sceneSecurityGroupRule.setToPort(port);
//			sceneSecurityGroupRule.setFromPort(port);
//		}
//
//
//		SecuritygroupRule sr_new = srtest.createSecuritygroupRule(srtt);
//
//		sceneSecurityGroupRule.setId(sr_new.getSrId());
//		sceneSecurityGroupRule.setTenantId(sr_new.getSrTenantId());
//		sceneSecurityGroupRule.setProjectId(sr_new.getSrProjectId());
//		sceneSecurityGroupRuleService.preInsert(sceneSecurityGroupRule);
//		sceneSecurityGroupRuleService.insert(sceneSecurityGroupRule);

		if(remoteChoose.equals("1"))
		{
			sceneSecurityGroupRule.setRemoteIpPrefix(null);
		}
		else {
			sceneSecurityGroupRule.setRemoteGroupId(null);
		}

		if(portChoose.equals("0"))
		{
			sceneSecurityGroupRule.setToPort(port);
			sceneSecurityGroupRule.setFromPort(port);
		}
		sceneSecurityGroupRuleService.save(sceneSecurityGroupRule);

		SceneSecurityGroupRule temp=new SceneSecurityGroupRule();
		temp.setSecurityGroupId(sceneSecurityGroupRule.getSecurityGroupId());
		Page<SceneSecurityGroupRule> page = sceneSecurityGroupRuleService.findPage(new Page<SceneSecurityGroupRule>(request, response), temp);
		model.addAttribute("page", page);

		model.addAttribute("sceneSecurityGroupId", sceneSecurityGroupRule.getSecurityGroupId());

		return "modules/scene/sceneSecurityGroupRuleList";
	}
}